#include "Obiect2D.h"

Obiect2D::Obiect2D(Tip2D t)
{
	tip = t;
	selected = false;
	translat[0] = translat[1] = 0;
	scalare[0] = scalare[1] = 1.0f;
	rotatie[0] = rotatie[1] = 1.0f;
	drawLines = 0;
	coords[0].z = coords[1].z = coords[2].z = coords[3].z = 0.0;
};

void Obiect2D::select(void)
{
	selected = true;
};
void Obiect2D::deselect(void)
{
	selected = false;
};
void Obiect2D::SetPozitie(float x, float y)
{
	pozitie[0] = x;
	pozitie[1] = y;
};
void Obiect2D::SetTranslat(float x, float y)
{
	translat[0] = x;
	translat[1] = y;
};
void Obiect2D::SetScalare(float x, float y)
{
	scalare[0] = x;
	scalare[1] = y;
};
void Obiect2D::SetRotatie(float x, float y)
{
	rotatie[0] = x;
	rotatie[1] = y;
};
void Obiect2D::SetCuloare(float r, float g, float b)
{
	culoare[0] = r;
	culoare[1] = g;
	culoare[2] = b;
};

void Obiect2D::SetCuloare_sel(float r, float g, float b)
{
	culoare_sel[0] = r;
	culoare_sel[1] = g;
	culoare_sel[2] = b;
};
void Obiect2D::SetDrawLines(int t)
{
	drawLines = t;
};
void Obiect2D::SetCoords(float *x , float *y, float *z)
{
	coords[0].x = x[0];
	coords[0].y = y[0];
	coords[0].z = z[0];
	coords[1].x = x[1];
	coords[1].y = y[1];
	coords[1].z = z[1];
	coords[2].x = x[2];
	coords[2].y = y[2];
	coords[2].z = z[2];
	coords[3].x = x[3];
	coords[3].y = y[3];
	coords[3].z = z[3];
};
float* Obiect2D::GetPozitie()
{
	return &pozitie[0];
};
float* Obiect2D::GetTranslat()
{
	return &translat[0];
};
float* Obiect2D::GetScalare()
{
	return &scalare[0];
};
float* Obiect2D::GetRotatie()
{
	return &rotatie[0];
};
float* Obiect2D::GetCuloare()
{
	return &culoare[0];
};
float* Obiect2D::GetCuloare_sel()
{
	return &culoare_sel[0];
};
Tip2D Obiect2D::GetTip()
{
	return tip;
};
void Obiect2D::SetTip(Tip2D t)
{
	tip = t;
};
void Obiect2D::draw()
{
	glPushMatrix();

	//glMatrixMode(GL_MODELVIEW);
	//glLoadIdentity();

	// translatie
	/*glTranslatef(translat[0], translat[1],0.0);

	// rotatie
	glRotatef( rotatie[0] , 1.0 , 0.0 , 0.0 );
	glRotatef( rotatie[1] , 0.0 , 1.0 , 0.0 );

	// scalare
	glScalef( scalare[0] , scalare[1] , 1.0);*/

	float a[4];
	a[0] = a[1] = a[2] = 1.0f;
	a[3] = 1.0f;
	float cul[4];
	for (int i = 0 ; i < 3 ; i++)
		cul[i] = culoare[i]*5;
	cul[3] = 1.0f;
	glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,cul);
	glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,a);
	glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,a);
	glColor3f(culoare[0], culoare[1], culoare[2]);
	//glOrtho(0.0,10.0f,10.0f,0,0.0f,100.0f);
	switch( tip )
	{
	case Line :	
		if (drawLines == 1) {
			glLineWidth (0.1f);
			glColor3f(1.0, 1.0, 1.0);
			glPushAttrib (GL_LINE_BIT);
			glLineStipple (3, 0xAAAA);
			glEnable(GL_LINE_STIPPLE);
			glBegin(GL_LINES);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(0,coords[0].y,0.0);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(coords[0].x,0,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(0,coords[1].y,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(coords[1].x,0,0.0);
			glEnd ();
			glPopAttrib ();
		}
		glColor3f(culoare[0], culoare[1], culoare[2]);
		glLineWidth (2.0f);
		glBegin(GL_LINES);
		glVertex3f(coords[0].x,coords[0].y,coords[0].z); 
		glVertex3f(coords[1].x,coords[1].y,coords[1].z);
		glEnd();

		break;
	case Triangle :	

		glColor3f(culoare[0], culoare[1], culoare[2]);
		glLineWidth (2.0f);
		glBegin(GL_TRIANGLES);
		glVertex3f(coords[0].x,coords[0].y,0.0); 
		glVertex3f(coords[1].x,coords[1].y,0.0);
		glVertex3f(coords[2].x,coords[2].y,0.0);
		glEnd();
		if (drawLines == 1) {
			glLineWidth (0.1f);
			glColor3f(1.0, 1.0, 1.0);
			glPushAttrib (GL_LINE_BIT);
			glLineStipple (3, 0xAAAA);
			glEnable(GL_LINE_STIPPLE);
			glBegin(GL_LINES);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(0,coords[0].y,0.0);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(coords[0].x,0,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(0,coords[1].y,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(coords[1].x,0,0.0);
			glVertex3f(coords[2].x,coords[2].y,0.0); 
			glVertex3f(0,coords[2].y,0.0);
			glVertex3f(coords[2].x,coords[2].y,0.0); 
			glVertex3f(coords[2].x,0,0.0);
			glEnd ();
			glPopAttrib ();
		}
		break;
	case Quad :	glBegin(GL_QUADS);
		glVertex3f(coords[0].x,coords[0].y,0.0); 
		glVertex3f(coords[1].x,coords[1].y,0.0);
		glVertex3f(coords[2].x,coords[2].y,0.0);
		glVertex3f(coords[3].x,coords[3].y,0.0);
		glEnd();
		if (drawLines == 1) {
			glLineWidth (0.1f);
			glColor3f(1.0, 1.0, 1.0);
			glPushAttrib (GL_LINE_BIT);
			glLineStipple (3, 0xAAAA);
			glEnable(GL_LINE_STIPPLE);
			glBegin(GL_LINES);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(0,coords[0].y,0.0);
			glVertex3f(coords[0].x,coords[0].y,0.0); 
			glVertex3f(coords[0].x,0,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(0,coords[1].y,0.0);
			glVertex3f(coords[1].x,coords[1].y,0.0); 
			glVertex3f(coords[1].x,0,0.0);
			glVertex3f(coords[2].x,coords[2].y,0.0); 
			glVertex3f(0,coords[2].y,0.0);
			glVertex3f(coords[2].x,coords[2].y,0.0); 
			glVertex3f(coords[2].x,0,0.0);
			glVertex3f(coords[3].x,coords[3].y,0.0); 
			glVertex3f(0,coords[3].y,0.0);
			glVertex3f(coords[3].x,coords[3].y,0.0); 
			glVertex3f(coords[3].x,0,0.0);
			glEnd ();
			glPopAttrib ();
		}
		break;
	}
	//glFlush();
	glPopMatrix();
};

Obiect2D::~Obiect2D(void)
{
};
